import React, { createContext, useContext, ReactNode } from 'react';

import useSubscribe from '../hook';

// 创建上下文
const SubscriptionContext = createContext<ReturnType<typeof useSubscribe> | undefined>(undefined);

// 提供者组件
export const SubscriptionProvider = ({ children }: { children: ReactNode }) => {
  const subscriptionHook = useSubscribe();

  return <SubscriptionContext.Provider value={subscriptionHook}>{children}</SubscriptionContext.Provider>;
};

// 自定义 Hook，用于访问上下文
export const useSubscriptionContext = () => {
  const context = useContext(SubscriptionContext);
  if (context === undefined) {
    throw new Error('useSubscriptionContext must be used within a SubscriptionProvider');
  }
  return context;
};
